﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;

namespace GXX.Pro.NetCore.Application.WebApi
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            #region 系统根目录
            var basePath = Microsoft.DotNet.PlatformAbstractions.ApplicationEnvironment.ApplicationBasePath;
            #endregion

            #region MVC
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
            #endregion

            #region Swagger UI Service
            services.AddSwaggerGen(o =>
            {
                o.SwaggerDoc("v1", new Info
                {
                    Version = "v0.1.0",
                    Title = "GXX.Pro.NetCore API",
                    Description = "接口文档说明",
                    TermsOfService="条款服务 http://www.tkfw.com",
                    Contact=new Contact
                    {
                        Name="GXX.Pro.NetCore",
                        Email="NetCore@xxx.com",
                        Url="http://www.zz.com"
                    }
                });


                #region 配置Xml信息
                Trace.WriteLine("basePath=" + basePath);
                Console.WriteLine("basePath=" + basePath);

                var xmlPath = Path.Combine(basePath, "GXX.Pro.NetCore.Application.WebApi.xml"); //接口说明
                o.IncludeXmlComments(xmlPath, true);

                var xmlModelPath = Path.Combine(basePath, "GXX.Pro.NetCore.Application.Model.xml");//model层说明
                o.IncludeXmlComments(xmlModelPath);

                Trace.WriteLine("xmlModelPath =  " + xmlModelPath);
                Console.WriteLine("xmlModelPath =  " + xmlModelPath);
                #endregion

                #region 添加header验证信息
                string bearer = "Bearer";
                 var security = new Dictionary<string, IEnumerable<string>> { { bearer, new string[] { } }, };
                o.AddSecurityRequirement(security);

                o.AddSecurityDefinition(bearer, new ApiKeyScheme
                {
                    Description = "JWT授权(数据将在请求头中进行传输) 参数结构: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",//jwt默认的参数名称
                    In = "header",//jwt默认存放Authorization信息的位置(请求头中)
                    Type = "apiKey"
                });
                #endregion
            });
            #endregion

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.Run(async (context) =>
            //{
            //    await context.Response.WriteAsync("Hello World!");
            //});


            #region Swagger
            app.UseSwagger();
            app.UseSwaggerUI(o =>
            {
                o.SwaggerEndpoint("/swagger/v1/swagger.json", "ApiHelp V1");

                o.RoutePrefix = ""; //生产/开发环境都可以

              
            });

            #endregion







            app.UseMvc();
        }
    }
}
